package persistencia;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.util.ArrayList;

import negocio.Cursada;
import negocio.Curso;
import negocio.Examen;
import negocio.ExamenControlador;
import negocio.Instructor;

public class CursadaDAO
{
	private static CursadaDAO instancia;
	private final Connection c;
	
	private CursadaDAO()
	{
		c = BDManager.getInstance().getConexion();
	}

	public static CursadaDAO getInstance()
	{
		if(instancia ==null)
		{
			instancia = new CursadaDAO();
		}
		return instancia;
	}
	
	public void guardar(Cursada cursada)
	{
		String query = "INSERT INTO Cursada(fechainicio, cantvacantesmax, cantvacantesmin, estadocursada, curso_fk, instructor_fk, horastotales) "
				+ "VALUES ('" + cursada.getFechaInicio().toString() +"'," + cursada.getMaxVacantes()+ "," + cursada.getMinVacantes() + ",'" 
				+ cursada.getEstado() + "'," + cursada.getCurso().getId() + ",'" + cursada.getInstructor().getDni() +"'," + cursada.getHoras() + ")";
		try {
			Statement s = c.createStatement();
			s.execute(query);
			s.close();
			this.crearNotaFinal(cursada);
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}

	}
	
	public Cursada getCursada(int id)
	{
		String query = "SELECT *FROM cursada cursada WHERE comision =" + id;
		Cursada res = null;
		try
		{
			Statement s = c.createStatement();
			ResultSet rs = s.executeQuery(query);
			rs.first();
			Curso curso= CursoDAO.getInstance().getCurso(rs.getInt("curso_fk"));
			Instructor instructor = InstructorDAO.getInstance().getInstructor(rs.getString("instructor_fk"));
			res = new Cursada(rs.getInt("comision"), curso, rs.getDate("fechainicio"), rs.getInt("cantvacantesmax"), 
					rs.getInt("cantvacantesmin"), instructor, rs.getString("estadocursada"), rs.getInt("horastotales"));
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return res;
	}
	
	public ArrayList<Cursada> getAll()
	{
		String query = "SELECT * FROM Cursada";
		Statement s;
		ResultSet rs;
		ArrayList<Cursada> res = new ArrayList<Cursada>();
		try {
			s = c.createStatement();
			rs = s.executeQuery(query);
			while(rs.next())
			{
				Curso curso= CursoDAO.getInstance().getCurso(rs.getInt("curso_fk"));
				Instructor instructor = InstructorDAO.getInstance().getInstructor(rs.getString("instructor_fk"));
				res.add(new Cursada(rs.getInt("comision"), curso, rs.getDate("fechainicio"), rs.getInt("cantvacantesmax"), 
						rs.getInt("cantvacantesmin"), instructor, rs.getString("estadocursada"), rs.getInt("horastotales")));
			}
			s.close();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return res;
	}
	
	public int getUltimoId()
	{
		String query = "Select max(comision) FROM cursada";
		int res = 0;
		try {
			Statement s = c.createStatement();
			ResultSet rs = s.executeQuery(query);
			rs.next();
			res = rs.getInt(1);
			s.close();
		} catch (SQLException e) {
			// TODO: handle exception
		}
		return res;
	}
	
	private void crearNotaFinal(Cursada cursada)
	{
		Examen notaFinal = new Examen(cursada, cursada.getFechaInicio(), "Nota final", this.getUltimoId());
		ExamenControlador controlador = new ExamenControlador();
		controlador.guardar(notaFinal);
	}
}
